﻿using Rescue.Services;
using RescueApp.Data;
using RescueApp.Models;
using RescueApp.Repository;
using RescueApp.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Rescue.UI
{
    public partial class Rescuesolve : Form
    {
        private RescueContext db;
        private RescueRepos repos;
        private RescueApplyService srv;
        private RescueDispatchService srv2;
        public Rescuesolve()
        {
            InitializeComponent();
        }
        public Rescuesolve(RescueContext db)
        {
            InitializeComponent();
            this.repos = new RescueRepos();
            this.db = db;
            srv = new RescueApplyService(db, repos);
            srv2 = new RescueDispatchService(db, repos);

        }
        private void button1_Click(object sender, EventArgs e)//填写救援表
        {
            int idx;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择你想要派遣救援的申请表！", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            idx = dataGridView1.SelectedRows[0].Index;
            int Id = int.Parse(dataGridView1.Rows[idx].Cells[0].Value.ToString());
            var todoitem = db.RescueApplies.FirstOrDefault(x => x.RescueID == Id);
            if (todoitem == null)
            {
                MessageBox.Show($"not Found the Id:{Id} Item!");
                return;
            }

            DispatchNewItem frm = new DispatchNewItem(db, srv2, todoitem);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Rescuesolve_Load(this, null);
            }
            frm.Close();

        }
        private void loadData(DataGridView dg)
        {

            List<RescueApply> todoItems;
            if (comboBox1.Text == "全部")
            {
                todoItems = srv.QueryAllItems();
            }
            else if (comboBox1.Text == "已派遣")
            {
                todoItems = srv.QueryAssignedItems();
            }
            else if (comboBox1.Text == "未派遣")
            {
                todoItems = srv.QueryUnassignedItems();
            }
            else//默认显示未派遣的申请表
            {
                todoItems = srv.QueryUnassignedItems();
                comboBox1.Text = "未派遣";
            }
            dg.DataSource = todoItems;
            dg.Refresh();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //当选择未派遣的申请表时，才可以填写对应的派遣表
            if (comboBox1.Text == "未派遣")
            {
                button1.Enabled = true;
            }
            else
            {
                button1.Enabled = false;
            }
        }

        private void Rescuesolve_Load(object sender, EventArgs e)
        {
            loadData(dataGridView1);//默认显示未派遣的申请表

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)//刷新按钮
        {
            Rescuesolve_Load(this, null);
        }
    }
}
